<?php
/**
 * 功能: 合伙人介绍
 * User: WD
 * Date: 2018/10/15 0015
 * Time: 15:42
 */

namespace app\admin\controller;
use app\base\controller\Power;
use think\Db;

class Partner extends Power{
    protected $beforeActionList = [
        'first'=> ['except'=>'index,rule'],

    ];
    protected function first(){
        $result = $this->testing();
    }
    /**
     * 合伙人介绍
     */
    public function index(){
        $res = Db::name('partner_intro')->find();
        if($res){
            $this->assign(['result'=>$res]);
        }
        return view('');
    }
    public function content_add(){
        $content = input('content');
        if(empty($content)){
            return json(['msg'=>'添加内容不能为空','ok'=>false]);
        }
        $res = Db::name('partner_intro')->insert(['content'=>$content]);
        if($res){
            return json(['msg'=>'添加成功','ok'=>true]);
        }else{
            return json(['msg'=>'添加失败','ok'=>false]);
        }
    }

    /**
     * 合伙人修改
     */
    public function content_edit(){
        $content = input('content');
        $rule_id = intval(input('rule_id'));
        if(empty($content)){
            return json(['msg'=>'添加内容不能为空','ok'=>false]);
        }
        $res = Db::name('partner_intro')->where('id',$rule_id)->update(['content'=>$content]);
        if($res){
            return json(['msg'=>'修改成功','ok'=>true]);
        }else{
            return json(['msg'=>'修改失败','ok'=>false]);
        }
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * 合伙人收益规则
     */
    public function rule(){
        $type = Db::name('partner_type')
            ->alias('pt')
            ->join('partner_rule pr','pt.id = pr.partner_type_id','left')
            ->field('pt.id,pt.name,pr.level1,pr.level2')
            ->select();

        $this->assign([
            'type'=>$type,
        ]);
        return view('');
    }

    /**
     * 设置规则
     */
    public function set_rule1(){
        $level1 = intval(input('level1'));
        $id     = intval(input('id'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数','ok'=>false]);
        }else{
            if($id==1){
                $status = 3;
            }else{
                $status = 4;
            }
        }
//        if(empty($level1)){
//            return json(['msg'=>'请填写完整的数据','ok'=>false]);
//        }
        $data = [
            'partner_type_id'=>$id,
            'status'=>$status,
            'level1'=>$level1,
        ];
        $check = Db::name('partner_rule')->where('partner_type_id',$id)->find();
        if($check){
            $res = Db::name('partner_rule')->where('partner_type_id',$id)->update($data);
        }else{
            $res = Db::name('partner_rule')->insert($data);
        }
        if($res){
            return json(['msg'=>'设置成功','ok'=>true]);
        }else{
            return json(['msg'=>'设置失败','ok'=>false]);
        }

    }
    public function set_rule2(){
        $level2 = intval(input('level2'));
        $id     = intval(input('id'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数','ok'=>false]);
        }else{
            if($id==1){
                $status = 3;
            }else{
                $status = 4;
            }
        }
//        if(empty($level2)){
//            return json(['msg'=>'数据不能为0或空','ok'=>false]);
//        }
        $data = [
            'partner_type_id'=>$id,
            'status'=>$status,
            'level2'=>$level2,
        ];
        $check = Db::name('partner_rule')->where('partner_type_id',$id)->find();
        if($check){
            $res = Db::name('partner_rule')->where('partner_type_id',$id)->update($data);
        }else{
            $res = Db::name('partner_rule')->insert($data);
        }
        if($res){
            return json(['msg'=>'设置成功','ok'=>true]);
        }else{
            return json(['msg'=>'设置失败','ok'=>false]);
        }

    }

}